﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SSN.DotNet
{
    /// <summary>
    /// Represents a dictionary that can be initialized once and read from, but cannot be changed.
    /// </summary>
    /// <typeparam name="K">Key type.</typeparam>
    /// <typeparam name="V">Value type.</typeparam>
    public class ReadOnlyDictionary<K, V> : IEnumerable<KeyValuePair<K, V>>
    {
        private Dictionary<K, V> _dictionary = null;

        /// <summary>
        /// Creates a new read only dictionary.
        /// </summary>
        /// <param name="dictionary">Dictionary to wrap as read only,</param>
        public ReadOnlyDictionary(Dictionary<K, V> dictionary)
        {
            _dictionary = dictionary;
        }

        /// <summary>
        /// Gets the item with teh given key, if it exists.
        /// </summary>
        /// <param name="key">Key.</param>
        /// <returns>Value of the key.</returns>
        public V this[K key]
        {
            get
            {
                if (_dictionary.ContainsKey(key))
                {
                    return _dictionary[key];
                }

                return default(V);
            }
        }

        /// <summary>
        /// Gets the number of key/value pairs contained in the ReadOnlyDictionary.
        /// </summary>
        public int Count
        {
            get { return _dictionary.Count; }
        }

        /// <summary>
        /// Gets a collection containing the keys in the ReadOnlyDictionary.
        /// </summary>
        public IEnumerable<K> Keys
        {
            get { return _dictionary.Keys; }
        }

        /// <summary>
        /// Gets a collection containing the values in the ReadOnlyDictionary.
        /// </summary>
        public IEnumerable<V> Values
        {
            get { return _dictionary.Values; }
        }

        /// <summary>
        /// Returns the enumerator for the read only dictionary.
        /// </summary>
        /// <returns>The enumerator.</returns>
        public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
        {
            return _dictionary.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _dictionary.GetEnumerator();
        }
    }
}
